package com.pro01.backend.service;

import com.pro01.backend.dto.CommentDTO;
import com.pro01.backend.mapper.Forum2CommentMapper;
import com.pro01.backend.pojo.Forum2Comment;
import com.pro01.backend.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Forum2CommentService {

    @Autowired
    private Forum2CommentMapper commentMapper;

    // 发表评论
    public int addComment(Forum2Comment comment) {
        int inserted = commentMapper.insertComment(comment);
        if (inserted > 0) {
            commentMapper.incrementCommentCount(comment.getPostId());
        }
        return inserted;
    }

    // 查看评论列表
    public PageResult<CommentDTO> getCommentsByPostId(Long postId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<CommentDTO> list = commentMapper.selectCommentsByPostId(postId, offset, pageSize);
        int total = commentMapper.countCommentsByPostId(postId);
        return new PageResult<>(total, list);
    }

    // 删除评论
    public int deleteComment(Long commentId, Long userId, String role) {
        Forum2Comment comment = commentMapper.getCommentOwnerAndPost(commentId);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }

        boolean isOwner = comment.getUserId().equals(userId);
        boolean isAdmin = "ADMIN".equals(role);

        if (!isOwner && !isAdmin) {
            throw new RuntimeException("无权限删除该评论");
        }

        int result = commentMapper.markCommentAsDeleted(commentId);
        if (result > 0) {
            int childCount = commentMapper.markChildCommentsAsDeleted(commentId);
            commentMapper.decrementCommentCountBy(comment.getPostId(), 1 + childCount);
        }
        return result;
    }

    // 隐藏/显示评论
    public int updateCommentVisibility(Long commentId, String status, Long userId, String role) {
        Long ownerId = commentMapper.getCommentOwnerId(commentId);
        if (ownerId == null) {
            throw new RuntimeException("评论不存在");
        }

        if (!"ADMIN".equals(role) && !ownerId.equals(userId)) {
            throw new RuntimeException("无权限修改该评论可见状态");
        }

        int updated = commentMapper.updateCommentStatus(commentId, status);

        // 如果是主评论，隐藏/显示它时也操作其所有未被删除的子评论
        if (updated > 0) {
            commentMapper.updateChildCommentStatus(commentId, status);
        }

        return updated;
    }

    /**
     * 接下来是管理员接口
     */
    // 查看所有评论
    public PageResult<CommentDTO> getAllCommentsPaged(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<CommentDTO> list = commentMapper.selectAllCommentsPaged(offset, pageSize);
        int total = commentMapper.countAllComments();
        return new PageResult<>(total, list);
    }

    // 删除任意评论
    public int deleteCommentAsAdmin(Long commentId) {
        Forum2Comment comment = commentMapper.getCommentOwnerAndPost(commentId);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }

        int deletedMain = commentMapper.markCommentAsDeleted(commentId);
        int deletedChild = commentMapper.markChildCommentsAsDeleted(commentId);

        // 更新评论计数（主评论 + 子评论）
        commentMapper.decrementCommentCountBy(comment.getPostId(), 1 + deletedChild);

        return deletedMain;
    }

    // 隐藏/显示所有评论
    public int updateCommentVisibilityAsAdmin(Long commentId, String status) {
        Forum2Comment comment = commentMapper.getCommentOwnerAndPost(commentId);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }

        int result = commentMapper.updateCommentStatus(commentId, status);
        if (result > 0) {
            commentMapper.updateChildCommentStatus(commentId, status);
        }
        return result;
    }

}
